package com.gcb.controller.order;

import com.gcb.common.annotation.Log;
import com.gcb.controller.BaseController;
import com.gcb.common.core.domain.AjaxResult;
import com.gcb.common.core.page.TableDataInfo;
import com.gcb.common.enums.BusinessType;
import com.gcb.common.utils.poi.ExcelUtil;
import com.gcb.domain.dto.OrderCargoQueryDTO;
import com.gcb.domain.dto.OrderCargoSaveDTO;
import com.gcb.domain.vo.OrderCargoVO;
import com.gcb.service.IOrderCargoService;
import com.gcb.framework.utils.SecurityUtils;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.Arrays;
import java.util.List;

/**
 * 货源订单管理
 */
@RestController
@RequestMapping("/orderCargo")
@Tag(name = "后台货源订单管理", description = "后台货源订单管理")
public class OrderCargoController extends BaseController {

    @Resource
    private IOrderCargoService orderCargoService;

    /**
     * 发布货源订单
     *
     * @param dto
     * @return
     */
    @PreAuthorize("@ss.hasPermi('order:orderCargo:add')")
    @PostMapping("/add")
    @Operation(summary = "发布货源订单")
    public AjaxResult add(@RequestBody OrderCargoSaveDTO dto) {
        orderCargoService.add(dto, SecurityUtils.getUserId());
        return success();
    }

    /**
     * 编辑货源订单
     *
     * @param dto
     * @return
     */
    @PreAuthorize("@ss.hasPermi('order:orderCargo:update')")
    @PostMapping("/update")
    @Operation(summary = "编辑货源订单")
    public AjaxResult update(@RequestBody OrderCargoSaveDTO dto) {
        orderCargoService.update(dto, SecurityUtils.getUserId());
        return success();
    }

    /**
     * 货源订单列表
     *
     * @param dto
     * @return
     */
    @PreAuthorize("@ss.hasPermi('order:orderCargo:pageList')")
    @PostMapping("/pageList")
    @Operation(summary = "货源订单列表")
    public TableDataInfo pageList(@RequestBody OrderCargoQueryDTO dto) {
        startPage();
        List<OrderCargoVO> list = orderCargoService.list(dto);
        return getDataTable(list);
    }

    /**
     * 货源订单详情
     *
     * @param id
     * @return
     */
    @PreAuthorize("@ss.hasPermi('order:orderCargo:detail')")
    @GetMapping("/detail")
    @Operation(summary = "货源订单详情")
    public AjaxResult detail(Long id) {
        OrderCargoVO detail = orderCargoService.getDetail(id);
        return success(detail);
    }

    /**
     * 批量修改货源订单状态
     *
     * @param ids
     * @return
     */
    @PreAuthorize("@ss.hasPermi('order:orderCargo:batchUpdateState')")
    @GetMapping("/batchUpdateState")
    @Operation(summary = "批量修改货源订单状态")
    public AjaxResult batchUpdateState(Long[] ids, Integer state) {
        orderCargoService.batchUpdateState(Arrays.asList(ids), state, SecurityUtils.getUserId());
        return success();
    }

    /**
     * 货源订单管理导出
     */
    @PreAuthorize("@ss.hasPermi('order:orderCargo:export')")
    @Log(title = "货源订单管理导出", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @Operation(summary = "货源订单管理导出")
    public void export(HttpServletResponse response, OrderCargoQueryDTO queryDTO) {
        List<OrderCargoVO> list = orderCargoService.list(queryDTO);
        ExcelUtil<OrderCargoVO> util = new ExcelUtil<>(OrderCargoVO.class);
        util.exportExcel(response, list, "货源订单管理导出");
    }

}
